using System;
using System.ComponentModel;
using System.Transactions;

using SIS.ENT;
using SIS.DAL;


namespace SIS.BLL
{

    /// <summary>
    /// The SubjectTableManager class is responsible for managing BO.SubjectTable objects in the system.
    /// </summary>
    [DataObjectAttribute()]
    public class SubjectTableManager
    {

        #region "Public Methods"

        /// <summary>
        /// Gets a list with all SubjectTable objects in the database.
        /// </summary>
        /// <returns>A list with all SubjectTable from the database when the database contains any, or null otherwise.</returns>
        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public static SubjectTableList GetList(int SchoolKey, int SchoolMajorKey, int StandardLevelKey)
        {
            return SubjectTableDB.GetList(SchoolKey, SchoolMajorKey, StandardLevelKey);
        }

        /// <summary>
        /// Gets a single SubjectTable from the database without its data.
        /// </summary>
        /// <param name="subjectKey">The SubjectKey of the SubjectTable in the database.</param>
        /// <returns>A SubjectTable object when the SubjectKey exists in the database, or <see langword="null"/> otherwise.</returns>
        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public static SubjectTable GetItem(int subjectKey)
        {
            return GetItem(subjectKey, false);
        }

        /// <summary>
        /// Gets a single SubjectTable from the database.
        /// </summary>
        /// <param name="subjectKey">The SubjectKey of the SubjectTable in the database.</param>
        /// <param name="getSubjectTableRecords">Determines whether to load all associated SubjectTable records as well.</param>
        /// <returns>
        /// A SubjectTable object when the SubjectKey exists in the database, or <see langword="null"/> otherwise.
        /// </returns>
        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public static SubjectTable GetItem(int subjectKey, bool getSubjectTableRecords)
        {
            SubjectTable mySubjectTable = SubjectTableDB.GetItem(subjectKey);
            //if (mySubjectTable != null && getSubjectTableRecords)
            //{
            //    mySubjectTable.courseDetailTables = CourseDetailTableDB.GetListBySubjectKey(subjectKey);
            //}
            return mySubjectTable;
        }

        /// <summary>
        /// Saves a SubjectTable in the database.
        /// </summary>
        /// <param name="mySubjectTable">The SubjectTable instance to save.</param>
        /// <returns>The new SubjectKey if the SubjectTable is new in the database or the existing SubjectKey when an item was updated.</returns>
        [DataObjectMethod(DataObjectMethodType.Update, true)]
        public static int Save(SubjectTable mySubjectTable)
        {
            using (TransactionScope myTransactionScope = new TransactionScope())
            {
                int subjectTableSubjectKey = SubjectTableDB.Save(mySubjectTable);
                //foreach (CourseDetailTable myCourseDetailTable in mySubjectTable.courseDetailTables)
                //{
                //    myCourseDetailTable.SubjectKey = subjectTableSubjectKey;
                //    CourseDetailTableDB.Save(myCourseDetailTable);
                //}

                //  Assign the SubjectTable its new (or existing SubjectKey).
                mySubjectTable.SubjectKey = subjectTableSubjectKey;

                myTransactionScope.Complete();

                return subjectTableSubjectKey;
            }
        }

        /// <summary>
        /// Deletes a SubjectTable from the database.
        /// </summary>
        /// <param name="mySubjectTable">The SubjectTable instance to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        [DataObjectMethod(DataObjectMethodType.Delete, true)]
        public static bool Delete(SubjectTable mySubjectTable)
        {
            return SubjectTableDB.Delete(mySubjectTable.SubjectKey);
        }

        /// <summary>
        /// Custom Function
        /// Deletes a SubjectTable from the database.
        /// </summary>
        /// <param name="SubjectKey">The SubjectTable instance to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        [DataObjectMethod(DataObjectMethodType.Delete, true)]
        public static bool Delete(int SubjectKey)
        {
            return SubjectTableDB.Delete(SubjectKey);
        }

        #endregion

    }

}
